package DMSXL._2024.DP;

public class lastStoneWeightII {
    public static void main(String[] args) {

    }
    public int lastStoneWeightII(int[] stones) {
        int all = 0;
        for (int i = 0; i < stones.length; i++) {
            all += stones[i];
        }
        int bagSize = all / 2;
        int[] dp = new int[bagSize + 1];
        for (int i = 0; i < stones.length; i++) {
            for (int j = bagSize; j >= stones[i] ; j--) {
                dp[j] = Math.max(dp[j],dp[j - stones[i]] + stones[i]);
            }
        }
        return all - 2 * dp[bagSize];
    }
}
